package com.ruicar.afs.cloud.manage.common.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruicar.afs.cloud.image.condition.OcrDiscernCondition;
import com.ruicar.afs.cloud.image.service.OcrDiscernService;
import com.ruicar.afs.cloud.manage.common.service.ContractOCRInterService;
import com.ruicar.afs.cloud.manage.common.vo.BankCardOcrDiscernVo;
import com.ruicar.afs.cloud.manage.common.vo.IdCardBackOcrDiscernVo;
import com.ruicar.afs.cloud.manage.common.vo.IdCardFrontOcrDiscernVo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @Author chuke
 * @create 2020/9/16 21:24
 */
@Service
@Slf4j
@AllArgsConstructor
public class ContractOCRInterServiceImpl implements ContractOCRInterService {
    /**
     * OCR识别
     */
    private final OcrDiscernService ocrDiscernService;

    /**
     * 银行卡识别
     *
     * @param condition
     * @return
     */
    @Override
    public BankCardOcrDiscernVo getBankCardOcrDiscern(OcrDiscernCondition condition) {
        JSONObject bankCard = ocrDiscernService.bankCardOcrDiscern(condition);
        if (bankCard != null) {
            log.info("*************************************调用接口记录入库结束**********************************");
            BankCardOcrDiscernVo bankCardOcrDiscernVo = new BankCardOcrDiscernVo();
            if (bankCard.getString("code").equals("200")) {
                bankCardOcrDiscernVo.setId(bankCard.getJSONObject("data").getString("id"));
                bankCardOcrDiscernVo.setBankCardNumber(bankCard.getJSONObject("data").getString("bankCardNumber"));
                bankCardOcrDiscernVo.setValidDate(bankCard.getJSONObject("data").getDate("vaildDate"));
                bankCardOcrDiscernVo.setBankCardType(bankCard.getJSONObject("data").getString("bankCardType"));
                bankCardOcrDiscernVo.setBankName(bankCard.getJSONObject("data").getString("bankName"));
                bankCardOcrDiscernVo.setBankCardMd5(bankCard.getJSONObject("data").getString("bankCardMd5"));
                bankCardOcrDiscernVo.setCreateTime(bankCard.getJSONObject("data").getDate("createTime"));
                return bankCardOcrDiscernVo;
            } else {
                log.info("接口调用参数出错");
                return null;
            }
        } else {
            return null;
        }
    }

    /**
     * 身份证正面照识别
     *
     * @param condition
     * @return
     */
    @Override
    public IdCardFrontOcrDiscernVo getIdCardFrontOcrDiscern(OcrDiscernCondition condition) {
        JSONObject idCardFront = ocrDiscernService.idCardFrontOcrDiscern(condition);
        if (idCardFront != null) {
            log.info("*************************************调用接口记录入库结束**********************************");
            IdCardFrontOcrDiscernVo idCardFrontOcrDiscernVo = new IdCardFrontOcrDiscernVo();
            if (idCardFront.getString("code").equals("200")) {
                idCardFrontOcrDiscernVo.setId(idCardFront.getJSONObject("data").getString("id"));
                idCardFrontOcrDiscernVo.setName(idCardFront.getJSONObject("data").getString("name"));
                idCardFrontOcrDiscernVo.setSex(idCardFront.getJSONObject("data").getString("sex"));
                idCardFrontOcrDiscernVo.setNation(idCardFront.getJSONObject("data").getString("nation"));
                idCardFrontOcrDiscernVo.setBirthday(idCardFront.getJSONObject("data").getDate("birthday"));
                idCardFrontOcrDiscernVo.setAddress(idCardFront.getJSONObject("data").getString("address"));
                idCardFrontOcrDiscernVo.setNumber(idCardFront.getJSONObject("data").getString("number"));
                idCardFrontOcrDiscernVo.setIdCardFrontMd5(idCardFront.getJSONObject("data").getString("idCardFrontMd5"));
                idCardFrontOcrDiscernVo.setCreateTime(idCardFront.getJSONObject("data").getDate("createTime"));
                return idCardFrontOcrDiscernVo;
            } else {
                log.info("接口调用参数出错");
                return null;
            }
        } else {
            return null;
        }
    }

    /**
     * 身份证反面识别
     *
     * @param condition
     * @return
     */
    @Override
    public IdCardBackOcrDiscernVo getIdCardBackOcrDiscern(OcrDiscernCondition condition) {
        JSONObject idCardBack = ocrDiscernService.idCardBackOcrDiscern(condition);
        if (idCardBack != null) {
            log.info("*************************************调用接口记录入库结束**********************************");
            IdCardBackOcrDiscernVo idCardBackOcrDiscernVo = new IdCardBackOcrDiscernVo();
            if (idCardBack.getString("code").equals("200")) {
                idCardBackOcrDiscernVo.setId(idCardBack.getJSONObject("data").getString("id"));
                idCardBackOcrDiscernVo.setValidPeriod(idCardBack.getJSONObject("data").getString("validPeriod"));
                idCardBackOcrDiscernVo.setLssue(idCardBack.getJSONObject("data").getString("lssue"));
                idCardBackOcrDiscernVo.setValidUntil(idCardBack.getJSONObject("data").getDate("validUntil"));
                idCardBackOcrDiscernVo.setLssueDate(idCardBack.getJSONObject("data").getDate("lssueDate"));
                idCardBackOcrDiscernVo.setIdCardBackMd5(idCardBack.getJSONObject("data").getString("idCardBackMd5"));
                idCardBackOcrDiscernVo.setCreateTime(idCardBack.getJSONObject("data").getDate("createTime"));
                return idCardBackOcrDiscernVo;
            } else {
                log.info("接口调用参数出错");
                return null;
            }
        } else {
            return null;
        }
    }

}
